clear; clc;
load('data/recsys/movielen100k.mat');

para.tau = 1.01;
[row, col, val] = find(data);
[m, n] = size(data);

clear user item data;

val = val - mean(val);
val = val/std(val);

idx = randperm(length(val));

traIdx = idx(1:floor(length(val)*0.5));
tstIdx = idx(ceil(length(val)*0.5): end);

clear idx;

traData = sparse(row(traIdx), col(traIdx), val(traIdx), m, n);

para.test.row  = row(tstIdx);
para.test.col  = col(tstIdx);
para.test.data = val(tstIdx);
para.test.m = m;
para.test.n = n;

%% start testing
para.maxIter = 5000;
para.decay = 0.8;
para.tol = 1e-2;

%% nuclear norm
lambda = 12;
% APG
method = 1;
t = tic;
[U, S, V, out{method,1}] = APGMatComp( traData, lambda, para );
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% SoftImpute
method = 2;
t = tic;
para.speedup = 1;
para.exact = 1;
[ U, S, V, out{method,1} ] = SoftImpute( traData', lambda, para);
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE(V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));

% ActiveSubspace
method = 3;
t = tic;
% [ U, S, V, out{3} ] = ActiveSubspace( traData', lambda, para);
[ U, S, V, out{method,1} ] = mc_alt( traData, lambda, para );
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% FastMatComp
t = tic;
method = 4;
para.maxR = 80;
para.speedup = 1;
para.regType = 1;
[U, S, V, out{method,1}] = FastMatComp( traData, lambda, 1e+8, para );
Time(method,1) = toc(t);
RMSE(method,1) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

clear U S V t;

%% Fixed Rank
estRank = 2;
method = 1;
t = tic;
[U, S, V, out{method,2} ] = SoftImputeALS( traData', 1e-6, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));

estRank = 5;
method = 2;
t = tic;
[U, S, V, out{method,2} ] = EOR1MP( traData, estRank, para );
Time(method,2) = toc(t);
RMSE(method,2) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% non-convex
para.tol = 1e-1;
for i = 3:5
    
    para.decay = 0.95;   
    para.regType = i - 2;
    para.maxR = 6;

    switch(para.regType)
        case 1
            lambda = 20;
            theta1 = lambda*2;
        case 2
            lambda = 1000;
            theta1 = sqrt(lambda);
        case 3
            lambda = 20;
            theta1 = 2;
    end

    % FAST
    method = 1;
    t = tic;
    para.speedup = 0;
    % [U, S, V, out{i,method} ] = IRNNMatCompSL( traData, lambda, theta1, para );
    % [U, S, V, out{i,method} ] = GISTMatCompSL( traData, lambda, theta1, para );
    [~, ~, ~, out{method,i}] = FastMatComp( traData, lambda, theta1, para );
    Time(method,i) = toc(t);
    RMSE(method,i) = out{method,i}.RMSE(end);

    method = 2;
    t = tic;
    para.speedup = 1;
    [~, ~, ~, out{method,i}] = FastMatComp( traData, lambda, theta1, para );
    Time(method,i) = toc(t);
    RMSE(method,i) = out{method,i}.RMSE(end);

    % GIST (fast)
    method = 3;
    t = tic;
    para.acc = 0;
    para.svdType = 0;
    [U, S, V, out{method,i}] = GISTMatComp( traData, lambda, theta1, para);
    Time(method,i) = toc(t);
    RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
    
    % GIST (slow)
    method = 4;
    t = tic;
    para.acc = 0;
    para.svdType = 1;
    [U, S, V, out{method,i}] = GISTMatComp( traData, lambda, theta1, para);
    Time(method,i) = toc(t);
    RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

    % IRNN
    method = 5;
    t = tic;
    [U, S, V, out{method,i}] = IRNNMatComp( traData, lambda, theta1, para);
    Time(method,i) = toc(t);
    RMSE(method,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

    clear U S V t method estRank;

end

clear U S V method t estRank;
clear i col row theta1 traData traIdx tstIdx user val data item para lambda;

save('ml100k-rst4')


